import json
import sqlite3
from copy import deepcopy

import pymysql
from createDataByInsert.ByInsertClass import InsertClass, logger, copyTable

from utils.environments_json import json_data
from utils.path_generator import filePath

def createTable(projectStr:str):
    if projectStr=="cpa":
        mysql_json = json_data.get(json_data.get("current_env")).get("mysql")
        server = mysql_json.get("server")
        port = int(mysql_json.get("port"))
        user = mysql_json.get("username")
        passwd = mysql_json.get("password")
        database = mysql_json.get("database")
        db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8')
        con = sqlite3.connect(filePath.sqlite_path + "DataByInsert.db")
        copyTable(con,db,filePath.sqlite_path + "TablesJSON.json")

    if projectStr=="user":
        mysql_json = json_data.get("userCenter" + json_data.get("current_env")).get("mysql")
        server = mysql_json.get("server")
        port = mysql_json.get("port")
        user = mysql_json.get("username")
        passwd = mysql_json.get("password")
        database = mysql_json.get("database")
        db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8')
        con = sqlite3.connect(filePath.sqlite_path + "userCenter.db")
        copyTable(con, db, filePath.sqlite_path + "userCenterTablesJSON.json")

    if projectStr == "wework":
        mysql_json = json_data.get("wework" + json_data.get("current_env")).get("mysql")
        server = mysql_json.get("server")
        port = mysql_json.get("port")
        user = mysql_json.get("username")
        passwd = mysql_json.get("password")
        database = mysql_json.get("database")
        db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8')
        con = sqlite3.connect(filePath.sqlite_path + "wework.db")
        copyTable(con, db, filePath.sqlite_path +"weworkbase.json")

    if projectStr == "shop":
        mysql_json = json_data.get("shop"+json_data.get("current_env")).get("mysql")
        server = mysql_json.get("server")
        port = int(mysql_json.get("port"))
        user = mysql_json.get("username")
        passwd = mysql_json.get("password")
        database = mysql_json.get("database")
        db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8')
        con = sqlite3.connect(filePath.sqlite_path + "shop.db")
        copyTable(con, db, filePath.sqlite_path + "shop.json")

    if projectStr == "live":
        mysql_json = json_data.get("live"+json_data.get("current_env")).get("mysql")
        server = mysql_json.get("server")
        port = int(mysql_json.get("port"))
        user = mysql_json.get("username")
        passwd = mysql_json.get("password")
        database = mysql_json.get("database")
        db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8')
        con = sqlite3.connect(filePath.sqlite_path + "live.db")
        copyTable(con, db, filePath.sqlite_path + "liveJson.json")

if __name__ == '__main__':
    createTable("cpa")
    # createTable("shop")
    # createTable("wework")
    # createTable("user")
    # createTable("live")


